package com.qf.Controller;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 你的名字
 * @Date: 2021/07/08/20:59
 * @Description:
 */
@RestController
@RequestMapping("file")
public class fileController {
    @Value("${fdfs.nginxHome}")
    private String fastdfspath;
    @Autowired
    private FastFileStorageClient ffsc;
    @RequestMapping("/upload")
    public String upload(MultipartFile file) throws IOException {
        System.out.println(file);
        //截取源图片的后缀
        String originalFilename = file.getOriginalFilename();

        int index = originalFilename.lastIndexOf(".");
        String houzhui = originalFilename.substring(index + 1);
        String uploadFile = "";
        try {
            StorePath storePath = ffsc.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), houzhui, null);
            uploadFile = storePath.getFullPath();
            System.out.println(uploadFile);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("file:"+fastdfspath+uploadFile);
        return fastdfspath+uploadFile;
    }
}


